//
//  PAAddCardViewController.m
//  PAMobileWallet
//
//  Created by Pay_SMP002 on 13-7-4.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PAAddCardViewController.h"
#import "Global.h"
#import "PAAddCardView.h"
#import "PADataCenter.h"
#import "PAChekText.h"

#import "PACardAuthViewController.h"
#import "PAPersonalViewController.h"
#import "PAStateType.h"


@interface PAAddCardViewController ()

@end

@implementation PAAddCardViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        addCardView = [[PAAddCardView alloc] initWithFrame:RECT_PHONE];
        addCardView.parentController = self;
        self.view = addCardView;
        
        navView = [[PANavView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        navView.delegate = self;
        [navView setTitle:@"开通快捷支付"];
        [addCardView addSubview:navView];
    }
    return self;
}

#pragma mark - PANavViewDelegate methods

- (void)sendSelf:(PANavView *)sendView
{
    if ([[PAStateType sharedInstance].personalOrCharge isEqualToString:@"20"]) {
        NSArray *controllers = [self.navigationController viewControllers];
        for (PABaseViewController *controller in controllers){
            if ([controller isKindOfClass:[PAPersonalViewController class]]){
                personalController = (PAPersonalViewController *)controller;
            }
        }
        [self.navigationController popToViewController:personalController animated:YES];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)testNext:(id)sender
{
    if ([PAChekText validBankCard:addCardView.cardNumTF.text] == 1)
    {
        PADataCenter *dataCenter = [PADataCenter sharedInstance];
        NSMutableDictionary *postingDic = [[NSMutableDictionary alloc] initWithDictionary:@{k_operationType: @"op_report_bank_card_num",k_loginId:dataCenter.self.selfInfo.baseInfo.account,k_bankCardNo:addCardView.cardNumTF.text}];
        [[PANetworkManager sharedInstance] reqAsynParamDic:postingDic userInfo:nil delegate:self];
    }
    else
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"银行卡必须是15-19位数字" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alert show];
    }
}

- (void)paRequestRespons:(NSDictionary *)responsDic userInfo:(NSDictionary *)userInfo
{
    NSString *op = [responsDic objectForKey:k_operationType];
    if ([op isEqualToString:@"op_report_bank_card_num"])
    {
        int bankCardType = [[responsDic objectForKey:@"bankCardType"] intValue];
        if (bankCardType == BCT_DEBIT_CARD){
            cardType = @"信用卡";
        }
        else if (bankCardType == BCT_CREDIT_CARD)
        {
            cardType = @"借记卡";
        }
        
        NSString * bankMark = [responsDic objectForKey:@"bankMark"];
        NSString * lastFourNum = [addCardView.cardNumTF.text substringFromIndex:[addCardView.cardNumTF.text length] - 4];
        NSString * cellName = [NSString stringWithFormat:@"尾号为 %@",lastFourNum];
        NSString * fullName = [NSString stringWithFormat:@"%@%@",[addCardView.cardNumTF.text substringToIndex:4],[addCardView.cardNumTF.text substringFromIndex:[addCardView.cardNumTF.text length] - 4]];
        if (!cardAuthController){
            cardAuthController = [[PACardAuthViewController alloc] initWithNibName:nil bundle:nil];
            cardAuthController.parentController = self;
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeStateType"  object:@"push"];
        [cardAuthController sendCardMark:bankMark sendCardNum:cellName sendType:cardType sendFullName:fullName];
        [self.navigationController pushViewController:cardAuthController animated:YES];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    addCardView.cardNumTF.text = @"";
    [addCardView.cardNumTF becomeFirstResponder];
    
    addCardView.nextButton.enabled = NO;
    [addCardView.nextButton setBackgroundImage:[UIImage imageNamed:@"button_B1.png"] forState:UIControlStateNormal];
    addCardView.nextButton.titleLabel.textColor = ENABLE_COLOR;

    [addCardView.cardNumTF addTarget:self action:@selector(changeCheckButtonState:) forControlEvents:UIControlEventEditingChanged];
}

- (void)changeCheckButtonState:(UITextField *)getUserNameTF
{
    if ([PAChekText validBankCard:addCardView.cardNumTF.text] == 1)
    {
        addCardView.nextButton.enabled = YES;
        [addCardView.nextButton setBackgroundImage:[UIImage imageNamed:@"button_A1.png"] forState:UIControlStateNormal];
        [addCardView.nextButton setBackgroundImage:[UIImage imageNamed:@"button_A2.png"] forState:UIControlStateHighlighted];
        [addCardView.nextButton setTitleColor:ABLE_COLOR forState:UIControlStateNormal];
        [addCardView.nextButton setTitleColor:ABLE_COLOR forState:UIControlStateHighlighted];
    }
    else
    {
        addCardView.nextButton.enabled = NO;
        [addCardView.nextButton setBackgroundImage:[UIImage imageNamed:@"button_B1.png"] forState:UIControlStateNormal];
        addCardView.nextButton.titleLabel.textColor = ENABLE_COLOR;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
